package com.ruoyi.quartz.task;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.enums.HtmlPageTypeEnum;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.generator.domain.model.FileGenInfo;
import com.ruoyi.generator.service.IGenHtmlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

import java.util.List;

@Component("generateHtmlTask")
public class GenerateHtmlTask {

    private final Logger log = LoggerFactory.getLogger(GenerateHtmlTask.class);

    @Autowired
    private IGenHtmlService genHtmlService;

    /**
     * 具体执行内容
     */
    public void execute() {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("generateHtmlTask");
        log.info("开始执行刷新HTML任务");
        // 清空目录下所有文件
        String htmlBasePath = RuoYiConfig.getHtmlGenPath();
        log.info("开始清空目录下所有HTML");
        FileUtils.deleteFileBySuffix(htmlBasePath, ".html");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/productIndex");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/productList");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/solutionIndex");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/solutionList");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/videoList");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/newsIndex");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/newsList");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/product");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/product2");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/video");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/news");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/about");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/support");
        FileUtils.forceDeleteDirectory(htmlBasePath + "/contact");
        log.info("清空目录下所有HTML结束");
        log.info("开始重新生成HTML");
        List<String> pageTypes = HtmlPageTypeEnum.allValues();
        for (String pageType : pageTypes) {
            FileGenInfo fileGenInfo = new FileGenInfo();
            fileGenInfo.setPageType(pageType);
            genHtmlService.generatorHTMLCode(fileGenInfo);
        }
        stopWatch.stop();
        log.info("重新生成HTML结束,刷新HTML任务完成,总耗时:{}秒", stopWatch.getTotalTimeSeconds());
    }

}
